package com.ubing.boot.notesforjava.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ubing.boot.notesforjava.dao.UserInfoMapper;
import com.ubing.boot.notesforjava.domel.entity.UserInfo;
import com.ubing.boot.notesforjava.response.Response;
import com.ubing.boot.notesforjava.service.IUserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ubing.boot.notesforjava.utils.CommonUtils;
import com.ubing.boot.notesforjava.utils.FileDownloadUtils;
import com.ubing.boot.notesforjava.utils.IdGeneratorSnowflake;
import com.ubing.boot.notesforjava.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ubing
 * @since 2023-02-17
 */
@Slf4j
@Service
public class IUserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService {
    @Autowired
    UserInfoMapper userInfoMapper;
    @Autowired
    IdGeneratorSnowflake snowflake;
    // token验证
    @Override
    public Response checkToken(HttpServletRequest request) {
        QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<UserInfo>().eq("user_id",request.getAttribute("id")).eq("user_name",request.getAttribute("userName"));
        UserInfo one = userInfoMapper.selectOne(userInfoQueryWrapper);
        return one==null?Response.failure("token验证失败，请重新登录"):Response.success(one);
    }

    // 用户登录，返回token
    @Override
    public Response login(UserInfo userInfo) {
        QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
        userInfoQueryWrapper.eq("user_name",userInfo.getUserName()).eq("password",userInfo.getPassword());
        UserInfo one = userInfoMapper.selectOne(userInfoQueryWrapper);
        log.info("{}",userInfo);
        one.setPassword("");
        log.info("登录信息:"+one);
        one.setToken(JwtUtils.createJWT(one.getUserId(),one.getUserName(),1000*60*60));
        return one==null?Response.failure("账号密码不对"):Response.success(one);
    }

    // 用户注册
    @Override
    public Response register(UserInfo userInfo) {
        userInfo.setUserId(snowflake.snowflakeId());
        CommonUtils.setCommon(userInfo);
        int insert = userInfoMapper.insert(userInfo);
        userInfo.setToken(JwtUtils.createJWT(userInfo.getUserId(),userInfo.getUserName(),1000*60*60));
        return insert>0?Response.success(userInfo):Response.failure("注册失败");
    }


    @Autowired
    private FileDownloadUtils fileDownloadUtils;

    // 头像文件上传
    @Override
    public Response avatarUpload(MultipartFile file,String name) throws IOException, InterruptedException {
//        String avatarName = FileDownloadUtils.downAvatar(file, snowflake.snowflakeId()+name);
        String avatarName =fileDownloadUtils.ThreadPoolDownAvatar(file, snowflake.snowflakeId()+name);
        return Response.success(avatarName);
    }

    //校验用户名是否重复
    @Override
    public Response validateUserName(UserInfo userInfo) {
        QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
        userInfoQueryWrapper.eq("user_name",userInfo.getUserName());
        List<UserInfo> userInfos = userInfoMapper.selectList(userInfoQueryWrapper);
        return userInfos.size()==0?Response.success():Response.failure("用户已存在");
    }
}
